package com.lagou.service.impl;


import com.lagou.base.StatusCode;
import com.lagou.dao.CourseContentDao;
import com.lagou.dao.impl.CourseContentDaoImpl;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.utils.DateUtils;

import java.util.List;

/**
 * 课程内容管路Service层实现类
 */
public class CourseContentServiceImpl implements CourseContentService {
    CourseContentDao courseContentDao = new CourseContentDaoImpl();

    //根据课程id查询课程内容
    @Override
    public List<Course_Section> findSectionAndLessonByCourseId(int courseId) {
        List<Course_Section> sections = courseContentDao.findSectionAndLessonByCourseId(courseId);
        return sections;

    }

    //根据id查询课程名称
    @Override
    public Course findCourseById(int courseId) {
        Course course = courseContentDao.findCourseByCourseId(courseId);
        return course;
    }

    //保存章节信息
    @Override
    public String saveSection(Course_Section section) {
        //1.补全章节信息
        section.setStatus(2); //状态，0:隐藏；1：待更新；2：已发布
        String date = DateUtils.getDateFormart();
        section.setCreate_time(date);
        section.setUpdate_time(date);
        //2.调用Dao进行插入
        int i = courseContentDao.saveSection(section);
        //根据是否插入成功,封装相应章节
        if (i > 0) {
            return StatusCode.SUCCESS.toString();
        } else {
            return StatusCode.FAIL.toString();
        }
    }

    @Override
    public String updateSection(Course_Section section) {

        //1.补全信息
        section.setUpdate_time(DateUtils.getDateFormart());
        //2.调用dao
        int row = courseContentDao.updateSection(section);
        //判断是否插入成功
        if (row > 0) {
            return StatusCode.SUCCESS.toString();
        } else {
            return StatusCode.FAIL.toString();
        }
    }

    //修改章节状态
    @Override
    public String updateSectionStatus(int id, int status) {
        //1.调用dao
        int row = courseContentDao.updateSectionStatus(id, status);
        //2.判断是否修改成功
        if (row > 0) {//修改状态成功
            return StatusCode.SUCCESS.toString();
        } else {//修改状态失败
            return StatusCode.FAIL.toString();
        }

    }

    //保存课时信息
    @Override
    public String saveLesson(Course_Lesson lesson) {
        //1.补全信息
        String dateFormart = DateUtils.getDateFormart();
        lesson.setCreate_time(dateFormart);
        lesson.setUpdate_time(dateFormart);
        //2.调用dao
        int row = courseContentDao.saveLesson(lesson);
        //3.判断是否修改成功
        if (row > 0) {//修改状态成功
            return StatusCode.SUCCESS.toString();
        } else {//修改状态失败
            return StatusCode.FAIL.toString();
        }
    }

    //修改课时信息
    @Override
    public String updateLesson(Course_Lesson lesson) {
        //1.补全信息
        lesson.setUpdate_time(DateUtils.getDateFormart());
        //2.调用Dao
        int row = courseContentDao.updateLesson(lesson);
        //3.判断是否修改成功
        if (row > 0) {//修改状态成功
            return StatusCode.SUCCESS.toString();
        } else {//修改状态失败
            return StatusCode.FAIL.toString();
        }
    }
}
